import 'package:flutter/material.dart';
import 'package:flutter_lang/lang_index.dart';
import 'package:provider/provider.dart';
import 'package:route_manager/wx_navigator.dart';
import 'package:wallet_home/model/wallet_list_model.dart';
import 'package:wallet_home/search/pages/custom_app_bar.dart';
import 'package:wallet_home/search/pages/search_bar.dart';
import 'package:wallet_home/tools/theme_utils.dart';
import 'package:wallet_home/widget/coins_manager.dart';
import '../module_config.dart';
import 'collection_qr_code.dart';


class CollectionSearchPage extends StatefulWidget {
  @override
  _CollectionSearchPageState createState() => _CollectionSearchPageState();
}

class _CollectionSearchPageState extends State<CollectionSearchPage> {
  List<CoinsBean> dataSources = [];
  FocusNode _focusNode = FocusNode();
  @override
  void initState() {
    super.initState();
    dataSources = CoinManager().allSelectedCoinList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(
          context,
          child:GestureDetector(
            child: SearchBar(
              focusNode: _focusNode,
              enabled: true,
              hideLeft: false,
              hideRight: false,
              hint: "${WXLang.$t("search_property",package: ModuleConfig.moduleName)}",
              leftButtonClick: () {
                Navigator.pop(context);
              },
              onChanged: (string){
                List<CoinsBean> list = <CoinsBean>[];
                for(CoinsBean coin in CoinManager().allSelectedCoinList){
                  if (coin.title.contains(string.toUpperCase())){
                    list.add(coin);
                  }
                }
                if(list.length > 0 ){
                  dataSources = list;
                  setState(() {

                  });
                }
              },
              rightWidget: Icon(
                Icons.add,
                color: Colors.grey,
                size: 26,
              ),
              rightButtonClick: (){
                _focusNode.unfocus();
                WXNavigator.pushNamed(routeName: '/wallet_search');
              },
            ),
            onTap: (){
              _focusNode.unfocus();
              WXNavigator.pushNamed(routeName: '/wallet_search_result');
            },
          ),
        ),
        body: GestureDetector(
          onTap: (){
            _focusNode.unfocus();
          },
          child: Container(
            color: ThemeUtils.getThemeColor(
                context: context,
              brightnessColor: Colors.white,
              darkColor: Colors.black12
            ),
            child: ListView.separated(
                  itemBuilder: (context,index){
                    return _buildListItem(dataSources[index]);
                  },
                  separatorBuilder: (context,index){
                    return Divider(
                      height: 0.5,
                      color: Colors.grey[200],
                    );
                  },
                  itemCount: dataSources.length ?? 0
              ),
          ),
        ),
    );
  }

  _buildListItem(CoinsBean bean) {
    return GestureDetector(
      child: Container(
        height: 60,
        color: Colors.white,
        width: double.infinity,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(left: 15),
              child:
              Image.asset(bean.img, width: 30, height: 30, fit: BoxFit.cover,package: ModuleConfig.moduleName),
            ),
            Padding(
                padding: EdgeInsets.only(left: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      bean.title,
                      style: TextStyle(color: Colors.grey[800], fontSize: 16),
                    ),
                    Text(
                      "≈${bean.cnyBalance} CNY",
                      style: TextStyle(color: Colors.grey[800], fontSize: 12),
                    )
                  ],
                )),
            Spacer(),
            Padding(
                padding: EdgeInsets.only(right: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      bean.num,
                      style: TextStyle(color: Colors.grey[800], fontSize: 16),
                    ),
                    Text(
                      "${bean.cnyMoney} CNY",
                      style: TextStyle(color: Colors.grey[800], fontSize: 12),
                    )
                  ],
                )
            ),
          ],
        ),
      ),
      onTap: (){
        WXNavigator.pushNamed(context: context, routeName: "/collection_qr_cod", arguments: bean);
      },
    );
  }
}
